<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1200" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
            <el-aside width="200px" v-loading="menuloading">
                <el-container>
                    <el-header>
                        <el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
                    </el-header>
                    <el-main class="nopadding">
                        <el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''" draggable highlight-current :expand-on-click-node="false" :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
                    </el-main>
                </el-container>
            </el-aside>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" @click="back">确定</el-button>
                        <el-button type="primary" icon="el-icon-plus" @click="add">添加</el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="search.keywords" placeholder="请输入关键词" clearable @change="upsearch"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                            <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" @select-all="selectAll" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="550">
                        <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                        <el-table-column type="index" label="#" align="center" width="50"></el-table-column>
                        <el-table-column label="编号" prop="code" align="center"></el-table-column>
                        <el-table-column label="名称" prop="name" align="center"></el-table-column>
                        <el-table-column label="检验标准" prop="standard" align="center"></el-table-column>
                        <el-table-column label="方法" prop="method" align="center"></el-table-column>
                        <el-table-column label="检验单位" prop="unit" align="center"></el-table-column>
                        <el-table-column label="标准值" prop="stvalue" align="center"></el-table-column>
                        <el-table-column label="检验上限" prop="upper" align="center"></el-table-column>
                        <el-table-column label="检验下限" prop="lower" align="center"></el-table-column>
                        <el-table-column label="备注" prop="note" align="center"></el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
  components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择不良品',
				edit: '选择检验项目',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			selection: [],
			apiObj: this.$API.production.item.list,
			search: {
				keywords: null,
			},
            dialog: {
                save: false
			},
            menuloading: false,
			menuFilterText: "",
            menuList: [],
			menuProps: {
				label: (data) => {
					return data.group_name
				}
			},
		}
	},
    watch: {
		menuFilterText(val) {
			this.$refs.menu.filter(val);
		}
	},
	mounted() {
		this.getMenu();
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.index = data
		},
        // 选择事件
        selectionChange(selection) {
            if(this.mode == 'bom'){
                if(selection.length > 1){
                    const del_row = selection.shift()
                    this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
                }
            }
			this.selection = selection;
		},
        selectAll() {
            if(this.mode == 'bom'){
                this.$message.error("只能单选！")
			    this.$refs.table.clearSelection()
            }
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.mode,this.index)
            this.visible = false;
        },
        //新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        // 重置
        clearSearch(){
            this.search.text_select = null
        },
        //加载树数据
		async getMenu() {
			this.menuloading = true;
			var res = await this.$API.production.itemType.list.get();
			this.menuloading = false;
			var allNode = {id: '', group_name: '所有'}
			res.data.unshift(allNode);
			this.menuList = res.data;
			// this.menuClick(allNode)
		},
		//树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				group: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.group_name.indexOf(value) !== -1;
		},
	}
}
</script>

<style></style>
